<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/19
 * Time: 9:30
 */

namespace App\Http\Controllers\Home;

use App\Models\Attention;
use App\Models\Coindetail;
use App\Models\FansGroup;
use App\Models\Gift;
use App\Models\Gift_recive;
use App\Models\Liverecord;
use App\Models\Liveroom;
use App\Models\Member;
use App\Models\Room_admin;
use App\Models\Roomgag;
use App\Models\Roomnum;

Class LiveController extends BaseController{

    public function __construct(Roomnum $roomnum , Liveroom $liveroom , Member $member , Gift $gift , Coindetail $coindetail , Liverecord $liverecord , Room_admin $room_admin , Roomgag $roomgag , Coindetail $coindetail, FansGroup $fansGroup , Attention $attention , Gift_recive $gift_recive)
    {
        parent::__construct();
        $this->room     = $roomnum;
        $this->liveroom = $liveroom;
        $this->member   = $member;
        $this->gift     = $gift;
        $this->coindetail = $coindetail;
        $this->liverecord =  $liverecord;
        $this->roomadmin = $room_admin;
        $this->roombag   = $roomgag;
        $this->coindetail = $coindetail;
        $this->fansgroup = $fansGroup;
        $this->attention = $attention;
        $this->giftrecive = $gift_recive;
    }

    public function getIndex($id){
        $data = $this->room->getRoomInfo($id);

        /**
         * 判断该房间是否存在
         */
        if ($data['isExist']==0){
            echo "该房间不存在";
            die;
        }

        if(session('loginId')) {
            $user = $this->member->userInit();
            // 加密后的用户信息
            $user['ecuid'] = xorencrypt(session('loginId'),'wecome');
            $user['nickname'] = $user['nickname'];
            $recivegift = $this->giftrecive->getGiftRecivedata(session('loginId'));


            // 判断是否为主播
            if ($data['uid']==session('loginId')) {
                $user['isZhubo'] = 1;
            }else{
                $user['isZhubo'] = 0;
            }

            // 判断是否为房管
            $user['isAdmin'] = $this->roomadmin->isRoomadmin(session('loginId'),$id);

            // 判断是否关注
            $attention = $this->attention->isAttention(['uid'=>session('loginId'),'attuid'=>$data['uid']]);
            $user['isAttent'] = $attention;
        }else{
            $user['islogin'] = 0;
        }

        /**
         * 是否开播
         */
        if ($data['isLive']) {
             // 获取拉流地址
             $data['playurl'] = $this->liveroom->getPlayUrl(config('app.tx.bizid'),$data['num']);
        }

        // 获取礼物列表
        $gift = $this->gift->getGiftList();

        // 排行榜
        // 贡献周榜
        $weekdata = $this->coindetail->weeklychart(['uid'=>$data['member']['id'],'room'=>$id]);
        $weekdata = $this->formatTop($weekdata);
        // 贵族

        // 粉丝榜
        $fansToplist = $this->fansgroup->FansToplist($id);
        $fansToplist = $this->formatTop($fansToplist);
        return view('Home.home.show',compact('data','user','gift','weekdata','fansToplist','recivegift'));
    }

    /**
     * 房间设置
     */
    public function getRoomset(){
        $user = $this->member->memberInfo('*',['id'=>session('loginId')]);
        $user['islogin'] = 1;
        $room = $this->room->getOne(['uid'=>session('loginId')]);
        return view('Home.home.onlinelive_setting',compact('room','user'));
    }

    public function getTest(){
        // 生成推流地址
        $push = $this->liveroom->getPushUrl(config('app.tx.bizid'),568456,config('app.tx.key'),date('Y-m-d H:i:s',time()+3600*24));
    }

    /**
     * 开始直播
     */
    public function getRoomLive($num){
        if (!session('loginId')){
            return redirect('index');
        }
        $user = $this->member->userInit();
        $data = $this->room->getRoomInfo($num);
        // 加密后的用户信息
        $user['ecuid'] = xorencrypt(session('loginId'),'wecome');
        $user['nickname'] = $user['nickname'];
        $showId = 0;
        // 是否开播
        if ($data['isLive']==1){
             // 获取拉流地址
             $data['playurl'] = $this->liveroom->getPlayUrl(config('app.tx.bizid'),$data['num']);
             // 获取本次开播id；
             $showId = $this->liverecord->getOnlineShowId($num);
        }
        // 获取礼物列表
        $gift = $this->gift->getGiftList();
        // 关联本场送的礼物数量
        $gift = $this->coindetail->getBycoindetail($gift,$showId);
        // 排行榜
        // 贡献周榜
        $weekdata = $this->coindetail->weeklychart(['uid'=>$data['member']['id'],'room'=>$num]);
        $weekdata = $this->formatTop($weekdata);
        // 贵族

        // 粉丝榜
        $fansToplist = $this->fansgroup->FansToplist($num);
        $fansToplist = $this->formatTop($fansToplist);
        return view('Home.home.liveroom',compact('data','user','gift','weekdata','fansToplist'));
    }

    /**
     * 保存房间设置
     */
    public function postSaveRoomSet(){
         $res = $this->room->saveRoomeSet(request());
         return redirect()->back()->with('msg', $res['msg'])->withInput();
    }

    /**
     * 开播
     */
    public function postStartLive(){
        $res = $this->liveroom->StartLive(request());
        echo json_encode($res);
    }

    /**
     * 关播
     */
    public function postEndLive(){
        $res = $this->liveroom->endLive(request());
        echo json_encode($res);
    }

    /**
     * 送礼物之前的检测
     */
    public function postSendgiftcheck(){
         $res = $this->member->isSendgift(request('gid'));
         echo json_encode($res);
    }

    /**
     * 添加房管
     */
    public function postAddroomadmin(){
       $res = $this->roomadmin->addRoomAdmin(request());
       echo json_encode($res);
    }

    /**
     * 添加用户禁言
     */
    public function postAddroombag(){
        $res = $this->roombag->addRoomgag(request());
        echo  json_encode($res);
    }

    /**
     * 判断是否被禁言
     */
    public function postIsRoomgag(){
         $res = $this->roombag->isRoomgag(request());
         echo json_encode($res);
    }

    /**
     * 格式化数据
     */
    public function formatTop($arr){
         if (empty($arr)) { return [];}
         $newArr = [];
         $newArr[0] = empty($arr[1])?0:$arr[1];
         $newArr[1] = $arr[0];
         $newArr[2] = empty($arr[2])?0:$arr[2];
         $arrl = count($arr)-3;
         if ($arrl<=0){
             return $newArr;
         }
         for ($i=3;$i<$arrl;$i++){
              $newArr[$i] = $arr[$i];
         }
         return $newArr;
    }

    /**
     * 添加关注
     */
    public function postAddatten(){
         $res = $this->attention->addAttention(request());
         echo json_encode($res);
    }

    /**
     * 添加关注
     */
    public function postDelatten(){
        $res = $this->attention->DelAttention(request());
        echo json_encode($res);
    }

    /**
     * 时间更新
     */
    public function postReceivegiftTimeUpdate(){
         $res = $this->giftrecive->updateTime();
         echo json_encode($res);
    }

    /**
     * 领取直播间在线礼物
     */
    public function postReceivegift(){
         $res = $this->giftrecive->receivegift();
         echo json_encode($res);
    }
}